package curator;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.atomic.AtomicValue;
import org.apache.curator.framework.recipes.atomic.DistributedAtomicInteger;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.retry.RetryNTimes;

/**
 * Created by Lenovo on 2018/11/22.
 */
public class CuratorAtomicInteger {

    /** zookeeper地址 */
    static final String CONNECT_ADDR = "10.1.66.76:2181";
    /** session超时时间 */
    static final int SESSION_OUTTIME = 5000;//ms

    public static void main(String[] args) throws Exception {

        //1 重试策略：初试时间为1s 重试10次
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10);
        //2 通过工厂创建连接
        CuratorFramework cf = CuratorFrameworkFactory.builder()
                .connectString(CONNECT_ADDR)
                .sessionTimeoutMs(SESSION_OUTTIME)
                .retryPolicy(retryPolicy)
                .build();
        //3 开启连接
        cf.start();
        //cf.delete().forPath("/super");

        //4 使用DistributedAtomicInteger
        //RetryNTimes 3，表示最大重试次数   1000，表示每次重试的间隔时间
        DistributedAtomicInteger atomicIntger =
                new DistributedAtomicInteger(cf, "/super", new RetryNTimes(3, 1000));

        //AtomicValue<Integer> value = atomicIntger.add(1);
        AtomicValue<Integer> value = atomicIntger.decrement();
        System.out.println(value.succeeded());
        System.out.println(value.postValue()); //最新值
        System.out.println(value.preValue());  //原始值
    }
}